<?php
/**
 * @分销
 * @功能说明:
 * @更新说明: 暂无更新
 * @文件名: BjieController.class.php
 * @编码: UTF-8
 * @创建时间: 2016-04-26 13:31
 * @创建人: WENCUN
 */

namespace dg\Controller;

class BjieController extends ApibaseController
{
    /*
     * 分销_宝洁商品
     * @date: 2016-04-26 13:33
     * @editor: WENCUN
     * */
    public function index() {
        $page = I('get.page') ?: 1;
        $perpage = 10;
        $url = array();
        $list = M('GoodsBjie');
        //统计品牌
        $this->brand = $list->distinct(true)->field(array('brand'))->where('brand <>""')->select();
        if (I('get.goods_title')) {
            $list = $list->where("goods_title LIKE '%" . I('get.goods_title') . "%'");
            $url['goods_title'] = I('get.goods_title');
        }
        if (I('get.brand')) {
            $list = $list->where(array('brand' => I('get.brand')));
            $url['brand'] = I('get.brand');
        }
        if (I('get.goods_skuid')) {
            $list = $list->where(array('goods_skuid' => I('get.goods_skuid')));
            $url['goods_skuid'] = I('get.goods_skuid');
        }
        if (I('get.status') != '') {
            if (I('get.status') == 1) {
                $list = $list->where('goods_id != 0');
            } else {
                $list = $list->where('goods_id = 0');
            }
            $url['status'] = I('get.status');
        }
        if (I('get.kcun') && I('get.kcun') == '1') {
            $list = $list->where(array('kcun' => '0'));
            $url['kcun'] = I('get.kcun');
        } else if (I('get.kcun') == '2') {
            $list = $list->where('kcun > 0');
            $url['kcun'] = I('get.kcun');
        }
        $object = clone $list;
        $getData = $list->order(array('add_at' => 'DESC'))->page($page, $perpage)->select();
        $this->assign('page', setPage($object->count(), $perpage, $page, '?' . http_build_query($url)));
        $this->assign('getData', $getData);
        $this->display('/bjie/index');
    }

    /*
     * 分销_宝洁商品编辑
     * @date: 2016-04-26 14:03:00
     * @editor: WENCUN
     * */
    public function goodsEdit() {
        if (IS_POST) {
            if (I('post.dosubmit') && I('post.goods_id')) {
                $GoodsId = M('GoodsBjie')->field(array('goods_id', 'goods_price', 'brand'))->where(array('id' => I('post.goods_id')))->find();
                if (!empty($GoodsId['goods_id'])) {
                    $this->showtips('该商品已入库');
                    exit;
                }
                //@todo 商品入库
                $data = array(
                    'goods_title'       => trim(I('post.goods_title')),
                    'goods_price'       => trim(I('post.goods_price')),
                    'channel_price'     => $GoodsId['goods_price'],
                    'brand'             => trim($GoodsId['brand']),
                    'refer_price'       => trim(I('post.refer_price')),
                    'goods_hb_price'    => trim(I('post.goods_hb_price')),
                    'goods_full_price'  => trim(I('post.goods_full_price')),
                    'goods_taxes_price' => trim(I('post.goods_taxes_price')),
                    'rate'              => trim(I('post.ratesnum')),
                    'rete_column'       => trim(I('post.rate')),
                    'goods_attr'        => trim(I('post.goods_attr')),
                    'resource'          => '2',
                    'kcun'              => trim(I('post.kcun')),
                    'goods_pic'         => trim(I('post.goods_pic')),
                    'goods_pic2'        => trim(I('post.goods_pic2')),
                    'goods_pic3'        => trim(I('post.goods_pic3')),
                    'hjprofit'          => trim(number_format(I('post.hjprofit'), 1, '.', '')),
                    'byprofit'          => trim(number_format(I('post.byprofit'), 1, '.', '')),
                    'zsprofit'          => trim(number_format(I('post.zsprofit'), 1, '.', '')),
                    'cate1_id'          => trim(I('post.cate1_id')),
                    'cate2_id'          => trim(I('post.cate2_id')),
                    'status'            => trim(I('post.status')),
                    'type'              => '1',
                    'taxes'             => I('post.taxes'),
                    'sort'              => trim(I('post.sort')),
                    'goods_content'     => htmlspecialchars(I('post.content')),
                    'add_at'            => time(),
                    'update_at'         => time()
                );
                $addId = M('GoodsEntity')->add($data);
                if ($addId) {
                    putlog('入库商品' . I('post.goods_title') . '`ID' . $addId . '`', 'KaolaController', 'goodsEdit');
                    M('GoodsBjie')->where(array('id' => I('post.goods_id')))->setField(array('goods_id' => $addId));
                    $this->showtips('入库商品成功');
                } else {
                    $this->showtips('操作失败');
                }
            }
        }
        if (I('get.id')) {
            $getData = M('GoodsBjie')->where(array('id' => I('get.id')))->find();
            //获取分类信息
            $cateMsg = M('CateEntity')->where(array('parent_id' => 0))->select();
            $this->assign('lowPrice', round(($getData['goods_price'] + 5) / 0.84, 1));
            $this->assign('getRateArr', skplus());
            $this->assign('cate', $cateMsg);
            $this->assign('getData', $getData);
            $this->display('/bjie/goodsedit');
        }
    }
}